<?php
/**
 * 
 * Save point
 * 
 * Allows the project to save to the file at any give save point in the
 * flex application. The only information taken is the XML file that is built up
 * in the application from the container classes.
 *
 * @author Nicola Wilkinson
 * 
 * @copyright 2007 Loughborough University
 * @license http://www.gnu.org/licenses/gpl.txt
 * @version 0.0.0.1
 * @since 2 Dec 2009
 * 
 */
 
 function logit($input) {
     $handle = fopen('__log.txt', "a");
     fwrite($handle, var_export($input, true) . "\n");
 }

 logit("------------------------------------------ LOGGING A SAVE POINT -------------------------------------");
 logit($_POST["myxml"]);
 
 //get the xml for the new project from the post.
 $new_project = $_POST["projectxml"];
 
 echo $new_project;
 
 //we need to write the xml to the server after extracting the project name.
 
 //extract the name
 $doc = new DOMDocument();
 $doc->loadXML($new_project);
 $project_name = $doc->getElementsByTagName( "name" );
 
 echo $new_project;
 
 //format the name -> remove capitals and any other characters not needed
  
 $project_name = $project_name->item(0)->nodeValue;
 $project_name = trim($project_name);
 $project_name = ereg_replace("[^A-Za-z0-9]", "", $project_name);
 
 echo "<br><br>" . $project_name;
 
 //create a string to point to the correct directory for the project with the refined name
 $path = dirname( __FILE__ );
 $fullPath = $path . '/../users/test' . '/' . $project_name;				//../users/test will be replaced by the username of the person using the tool!
 
 //delete the file, and save the new one
 $filepath = $fullPath . "/project.xml";
 chmod($filepath, 0777);
 //if (is_file($filepath)){
 //	unlink($filepath);
 //}
 
 //save the XML into the folder as the project.xml
 $doc->save($fullPath . "/project.xml");
 
logit("------------------------------------------ END OF LOGGING A SAVE POINT -------------------------------------");
?>